package com.heze.xunhuan.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.heze.xunhuan.config.AppConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * 阿里云OSS工具类（保留以供参考）
 *
 * @author heze-xunhuan
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OssUtil {

    private final AppConfig appConfig;

    /**
     * 上传文件到阿里云OSS
     *
     * @param file 文件
     * @param folder 文件夹名称
     * @return 文件访问URL
     */
    public String uploadFile(MultipartFile file, String folder) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例
            ossClient = new OSSClientBuilder().build(
                    appConfig.getFile().getOss().getEndpoint(),
                    appConfig.getFile().getOss().getAccessKeyId(),
                    appConfig.getFile().getOss().getAccessKeySecret());

            // 生成文件名
            String originalFilename = file.getOriginalFilename();
            String fileExtension = "";
            if (originalFilename != null && originalFilename.contains(".")) {
                fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            String fileName = folder + "/" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + 
                    "/" + UUID.randomUUID().toString().replace("-", "") + fileExtension;

            // 上传文件
            PutObjectRequest putObjectRequest = new PutObjectRequest(
                    appConfig.getFile().getOss().getBucketName(),
                    fileName,
                    file.getInputStream());
            ossClient.putObject(putObjectRequest);

            // 返回文件访问URL
            return "https://" + appConfig.getFile().getOss().getBucketName() + "." + 
                    appConfig.getFile().getOss().getEndpoint() + "/" + fileName;
        } catch (IOException e) {
            log.error("文件上传失败", e);
            throw new RuntimeException("文件上传失败", e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 删除阿里云OSS中的文件
     *
     * @param fileName 文件名
     */
    public void deleteFile(String fileName) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例
            ossClient = new OSSClientBuilder().build(
                    appConfig.getFile().getOss().getEndpoint(),
                    appConfig.getFile().getOss().getAccessKeyId(),
                    appConfig.getFile().getOss().getAccessKeySecret());

            // 删除文件
            ossClient.deleteObject(appConfig.getFile().getOss().getBucketName(), fileName);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}